package num58;

/**
 * 58. 最后一个单词的长度
 *
 * 题目： https://leetcode-cn.com/problems/length-of-last-word/
 * 三叶姐题解： https://leetcode-cn.com/problems/length-of-last-word/solution/gong-shui-san-xie-jian-dan-zi-fu-chuan-m-tt6t/
 *
 * 三叶姐解法
 */
class Solution2 {
    public int lengthOfLastWord(String s) {
        int n = s.length();
        int j = n - 1;
        while (j >= 0 && s.charAt(j) == ' ') {
            j--;
        }
        int i = j;
        while (i >= 0 && s.charAt(i) != ' ') {
            i--;
        }
        return j - i;
    }
}
